package com.aimosheng.dynamic.dialog;

import android.content.DialogInterface;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.dynamic.R;
import com.tencent.rtmp.ITXVodPlayListener;
import com.tencent.rtmp.TXLiveConstants;
import com.tencent.rtmp.TXVodPlayConfig;
import com.tencent.rtmp.TXVodPlayer;
import com.tencent.rtmp.ui.TXCloudVideoView;
import com.aimosheng.common.Constants;
import com.aimosheng.common.dialog.AbsDialogFragment;
import com.aimosheng.common.utils.DialogUitl;
import com.aimosheng.common.utils.WordUtil;
import com.aimosheng.dynamic.activity.DynamicPublishActivity;


/**
 * Created by debug on 2019/7/22.
 * 视频播放
 */

public class VideoDialogFragment extends AbsDialogFragment implements View.OnClickListener, ITXVodPlayListener {

    private TXVodPlayer mTXVodPlayer;
    private TXCloudVideoView mTXCloudVideoView;
    private String mVideoPath;

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_look_video;
    }

    @Override
    protected int getDialogStyle() {
        return R.style.dialog3;
    }

    @Override
    protected boolean canCancel() {
        return false;
    }

    @Override
    protected void setWindowAttributes(Window window) {
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        findViewById(R.id.btn_del).setOnClickListener(this);
        findViewById(R.id.btn_close).setOnClickListener(this);
        loadData();
    }

    private void loadData() {
        Bundle bundle = getArguments();
        if (bundle == null) {
            return;
        }
        mVideoPath = bundle.getString(Constants.VIDEO_PATH);
        mTXCloudVideoView = (TXCloudVideoView) findViewById(R.id.video_view);
        mTXCloudVideoView.setRenderMode(TXLiveConstants.RENDER_MODE_FULL_FILL_SCREEN);
        mTXVodPlayer = new TXVodPlayer(mContext);
        TXVodPlayConfig txVodPlayConfig = new TXVodPlayConfig();
        txVodPlayConfig.setMaxCacheItems(15);
        txVodPlayConfig.setProgressInterval(200);
        mTXVodPlayer.setConfig(txVodPlayConfig);
        mTXVodPlayer.setAutoPlay(true);
        mTXVodPlayer.setLoop(true);
        mTXVodPlayer.setVodListener(this);
        mTXVodPlayer.setPlayerView(mTXCloudVideoView);
        if (mTXVodPlayer != null) {
            mTXVodPlayer.startPlay(mVideoPath);
        }
    }

    private void del() {
        DialogUitl.showTitleStringArrayDialog(mContext, new Integer[]{
                R.string.delete
        }, false, WordUtil.getString(R.string.dynamic_video_del_tip), new DialogUitl.StringArrayDialogCallback() {
            @Override
            public void onItemClick(String text, int tag) {
                ((DynamicPublishActivity) mContext).delVideo();
                dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_del) {
            del();
        } else if (i == R.id.btn_close) {
            dismiss();
        }

    }


    @Override
    public void onPlayEvent(TXVodPlayer txVodPlayer, int i, Bundle bundle) {
        switch (i) {
            case TXLiveConstants.PLAY_EVT_CHANGE_RESOLUTION:
                onVideoSizeChanged();
                break;
        }
    }

    /**
     * 获取到视频宽高回调
     */
    public void onVideoSizeChanged() {
        double videoWidth = 0;
        double videoHeight = 0;
        MediaMetadataRetriever mmr = null;
        try {
            mmr = new MediaMetadataRetriever();
            mmr.setDataSource(mVideoPath);
            String width = mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);//宽
            String height = mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);//高
            String rotation = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
            if ("0".equals(rotation)) {
                if (!TextUtils.isEmpty(width)) {
                    videoWidth = Double.parseDouble(width);
                }
                if (!TextUtils.isEmpty(height)) {
                    videoHeight = Double.parseDouble(height);
                }
            } else {
                if (!TextUtils.isEmpty(height)) {
                    videoWidth = Double.parseDouble(height);
                }
                if (!TextUtils.isEmpty(width)) {
                    videoHeight = Double.parseDouble(width);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mmr != null) {
                mmr.release();
            }
        }
        if (mTXCloudVideoView != null && videoWidth > 0 && videoHeight > 0) {
            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mTXCloudVideoView.getLayoutParams();
            if (videoWidth / videoHeight > 0.5625f) {//横屏 9:16=0.5625
                params.height = (int) (mTXCloudVideoView.getWidth() / videoWidth * videoHeight);
                params.gravity = Gravity.CENTER;
                mTXCloudVideoView.requestLayout();
            }
        }
    }


    @Override
    public void onNetStatus(TXVodPlayer txVodPlayer, Bundle bundle) {

    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (mTXVodPlayer != null) {
            mTXVodPlayer.stopPlay(true);
            mTXVodPlayer.setVodListener(null);
        }
        mTXVodPlayer = null;
    }


}
